<?php

//define('Acache_no','1');
class Con_api extends Acan{
	
	public $defaultAct='userinfo';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		$this->Auth = $_POST['Auth'];//认证信息
		if($this->Auth['uid']>0){
			$this->uid = $this->Auth['uid'];
			$this->fid = $this->Auth['fid'];
		}
		if(!in_array($this->Aact,array('reg','find_fate','find_info'))){
			$this->auth();
		}
		$this->_S = Afun_session();
	}
	//验证请求
	function auth(){
		if($_SERVER["SERVER_PORT"]!=80){
			$port = ':'.$_SERVER['SERVER_PORT'];
		}
		if($_SERVER['HTTP_HOST']=='127.0.0.1'.$port && $_SERVER['REMOTE_ADDR']=='127.0.0.1') return;//请求主机是本地,请求来源是本地
		$post = $_POST;
		$this->oid = $post['oid'];
		$this->data = $post['data'];
		$this->info = $post['info'];
		$this->post = $post;
		if($this->Auth['hash']!=Afun_auth_hash($this->Auth['uid'],$this->Auth['time'])){
			$this->Auth['status'] = 0;
			$this->status = 0;
			return $this->recv("验证失败".$_SERVER["HTTP_HOST"].$_SERVER["REMOTE_ADDR"]);
		}else{
			$this->status = 1;
		}
	}
	//返回信息
	function recv($obj){
		$data->fid = $this->fid;
		$data->oid = $this->oid;
		$data->uid = $this->uid;
		$data->Auth = $this->Auth;
		$data->status = $this->status;
		$data->data = $obj;
		Afun_json($data);
	}
	//同步信息
	function Act_sync(){
		//同步用户登入表
		$user = Afun_db()->model(Acan_table_weibo_user);
		$data = $user->ofindPk($this->oid);
		if(!$data){
			$data->oid = $this->oid;
			$data->lastdate = Afun_time_date($this->Auth['time']);
			$data->lastip = $this->ip;
			$data->regdate = $data->lastdate;
			$data->regip = $data->lastip;
			$data->status = $user->save($data,1);
		}else{
			$data->lastip = $this->ip;
			$data->lastdate = Afun_time_date($this->Auth['time']);
			$data->status = $user->save($data);
		}
		//同步用户信息表
		$model = Afun_db()->model(Acan_table_weibo_userinfo);
		$datas = $model->find("oid=$this->oid");
		if(!$datas){
			$datas->oid = $this->oid;
			$datas->username = $this->info[screen_name];
			$datas->area = Afunc_area()->encode(str_replace(' ',',',$this->info['location']));//地区转码
			$datas->avatar_min = $this->info[profile_image_url];
			$datas->avatar = $this->info[avatar_large];
			$datas->domain = $this->info[domain];
			$datas->fans = $this->info[followers_count];//粉丝数
			$datas->follow = $this->info[friends_count];//关注数
			$datas->weibo = $this->info[statuses_count];//微博数
			$datas->status = 2;
			$datas->sex = Afun_new('weibo')->sex_decode($this->info[gender]);
			$model->save($datas);
		}
		//第一次登入需要填写
		if($data->ready==0){
			
		}
		$obj->user = $data;
		$obj->info = $datas;
		$this->recv($obj);
	}
	//注册用户
	function Act_reg(){
		if($_POST){
			$post = $_POST;
			$user = $post['user'];
			$user['mail'] = $user['username'];
			$user['regip'] = $this->Auth['ip'];
			$rs = Afunc_user()->add($user);
			if($rs->status == 1){
				$save = $post['fate'];
				$save['uid'] = $rs->data['uid'];
				$zodiac = Afun_new("fate")->shengxiao(date("Y",strtotime($save['birthday'])),2);
				$xingzuo = Afun_new("fate")->xingzuo(date("m-d",strtotime($save['birthday'])),2);
				$save['zodiac'] = $zodiac['enum'];
				$save['xingzuo'] = $xingzuo['enum'];
				$area = $save['area'];
				$save['sheng'] = $area[1];
				$save['shi'] = $area[2];
				$save['xian'] = $area[3];
				unset($save['area']);
				$mfate = Afun_db()->model(Acan_table_fate);
				$ed = $mfate->find("uid=".$save['uid']);
				if($ed){
					$mfate->update($save,"uid=".$save['uid']);//存在记录更新
				}else{
					$mfate->save($save);
				}
			}
			$this->Auth['uid'] = $rs->data['uid'];
			$this->Auth['time'] = Afun_get_time("time");
			$this->Auth['hash'] = Afun_auth_hash($this->Auth['uid'],$this->Auth['time']);
			$this->recv($rs);
		}else{
			$this->recv(false);
		}
	}
	function Act_user_save(){
		if($this->uid<1){
			$this->recv(false);
		}
		if($_POST){
			$post = $_POST;
			if($post['user']){
				$user = $post['user'];
				$user['uid'] = $this->uid;
				$rs = Afunc_user()->save($user);
			}
			if($post['fate']){
				$save = $post['fate'];
				$save['uid'] = $this->uid;
				$zodiac = Afun_new("fate")->shengxiao(date("Y",strtotime($save['birthday'])),2);
				$xingzuo = Afun_new("fate")->xingzuo(date("m-d",strtotime($save['birthday'])),2);
				$save['zodiac'] = $zodiac['enum'];
				$save['xingzuo'] = $xingzuo['enum'];
				$area = $save['area'];
				$save['sheng'] = $area[1];
				$save['shi'] = $area[2];
				$save['xian'] = $area[3];
				unset($save['area']);
				$mfate = Afun_db()->model(Acan_table_fate);
				$ofate = $mfate->find("uid=$this->uid");
				if($ofate['fid']>0){
					$rs = $mfate->updatePk($save,$ofate['fid']);//存在记录更新
				}else{
					$rs = $mfate->save($save);//存在记录更新
				}
			}
		}
		$obj->rs = $rs;
		$obj->ss = $save;
		$obj->msg = '更新信息成功';
		$this->recv($obj);
	}
	//第一次信息存储
	function Act_readysave(){
		if(is_array($_POST)){
			$post = $_POST;
			$model = Afun_db()->model(Acan_table_weibo_userinfo);
			$rs = $model->update($post,"oid=$this->oid");
			Afun_db()->model(Acan_table_weibo_user)->updatePk("ready=1",$this->oid);
			$msg = '保存成功';
		}else{
			$rs = -1;
			$msg = '保存失败';
		}
		$obj->status = $rs;
		$obj->msg = $msg;
		$this->recv($obj);
	}
	function Act_find_fate(){
		$post = $_POST;
		$mfate = Afun_db()->model(Acan_table_fate);
		$fnum = Afun_cache_get("Afate_num");
		if(!$fnum){
			$fnum = $mfate->count();
			Afun_cache_set("Afate_num",$fnum);
		}
		$fnum = rand(1,$fnum-1000);
		$where = "1";
		$birthday = $post['birthday'];
		$selftime = strtotime($birthday);
		$year = date("Y",$selftime);
		$sex = $post['sex'];
		$where .= " and a.sex=$sex";//查询异性的
		$where .= " and a.fid > $fnum";
		if(in_array(1,$post['fid'])){
			$zodiac = Afun_new("fate")->shengxiao(date("Y",strtotime($birthday)),2);
			//生肖大吉配表
			$sxfate = array(
				"1"=>array('2','5','9'),
				"2"=>array('1','6','10'),
				"3"=>array('7','11'),
				"4"=>array('8','11','12'),
				"5"=>array('1','9','10'),
				"6"=>array('2','10'),
				"7"=>array('3','8','11'),
				"8"=>array('4','7','12'),
				"9"=>array('1','5'),
				"10"=>array('2','5','6'),
				"11"=>array('3','4','7'),
				"12"=>array('4','8')
			);
			$sx = $sxfate[$zodiac['enum']];
			for($i=0;$i<count($sx);$i++){
				if(empty($str))
					$str = $sx[$i];
				else
					$str .= ','.$sx[$i];
			}
			$where .= " and a.zodiac in ($str)";
		}
		$where .= " and a.birthday >'".($year-10)."-01-01 00:00:00' and a.birthday <'".($year+10)."-01-01 00:00:00'";
		if(in_array(2,$fid)){
			$xingzuo = Afun_new("fate")->xingzuo(date("m-d",strtotime($birthday)),2);
		}
		$con->select = "a.*,i.username,i.follow,i.fans,i.weibo";
		if($type=='uid' && $str>0){
			$where = " and a.uid=$str";//指定UID查找
		}
		$con->where = $where;
		$con->limit = 10;
		$con->join = "LEFT JOIN `".Acan_table_weibo_userinfo."` i on a.oid=i.oid";
		$data = $mfate->ofindAll($con);
		/*
		八字信息
		foreach ($data as $key => $val) {
			$data[$key]->info = Afun_new('fate')->info($val->birthday);
		}*/
		$this->recv($data);
	}
	function Act_find_info(){
		$post = $_POST;
		$obj = Afun_new('fate')->info($post['birthday']);
		$this->recv($obj);
	}
	//发送私信
	function Act_poll_chat(){
		if($this->uid<1){$this->recv(false);}
		$post = $_POST;
		$queue = $post['queue'];
		$cmodel = Afun_db()->model(Acan_table_fate_chat);
		if(is_array($queue))
		foreach ($queue as $key => $val) {
			$rs = Afun_new('weibo')->sendmsg($val['toid'],$val['note']);
			$val['foid'] = $this->Auth->oid;
			$val['chatid'] = $rs->chatid;
			$cmodel->save($val);
			unset($val['note']);
			$chat[] = $val;
		}
		$obj->chat = $chat;
		$this->recv($obj);
	}
	//获取用户信息
	function Act_userinfo(){
		if($this->uid<1){$this->recv(false);}
		$uid = $this->uid;
		$mfate = Afun_db()->model(Acan_table_fate);
		$minfo = Afun_db()->model(Acan_table_weibo_userinfo);
		$data = Afunc_user()->info($uid);
		$data->fate = $mfate->ofind("uid=$uid");
		if($data->fate->oid>0){
			$oid = $data->fate->oid;
			$info = $minfo->ofind("oid=$oid");
			$data->info = $info;
		}
		$this->recv($data);
	}
	function Act_user_find(){
		$post = $_POST;
		if($post['mail']){
			$mail = $post['mail'];
			$muser = Afun_db()->model(Acan_table_user);
			$con->where = "mail='$mail'";
			$rs = $muser->find($con);
			if($rs['uid']>0){
				$mfate = Afun_db()->model(Acan_table_fate);
				$con->select = "a.*,i.username,i.follow,i.fans,i.weibo";
				$con->where = " a.uid=".$rs['uid'];//指定UID查找
				$con->limit = 10;
				$con->join = "LEFT JOIN `".Acan_table_weibo_userinfo."` i on a.oid=i.oid";
				$data = $mfate->ofind($con);
			}else{
				$data = 0;
			}
			$this->recv($data);
		}
	}
	//添加好友
	function Act_add_friend(){
		if($this->uid<1){$this->recv(false);}
		$post = $_POST;
		if($post['fid']>0){
			$fid = $post['fid'];
			$bsave->type = 1;//绑定的关系类型
			$bsave->fid = $this->fid;
			$bsave->bfid = $fid;
			$mbind = Afun_db()->model(Acan_table_fate_bind);
			$rs = $mbind->save($bsave);
			if($rs==1){
				$obj->msg = '添加成功!';
			}else{
				$obj->msg = '添加失败!';
			}
		}else{
			$obj->msg = '请先选择用户!';
		}
		$this->recv($obj);
	}
	function Act_friend(){
		if($this->uid<1){$this->recv(false);}
		$mbind = Afun_db()->model(Acan_table_fate_bind);
		$con->select = "b.*,i.username,i.follow,i.fans,i.weibo";
		$con->where = 'a.type=1 and a.fid='.$this->fid;
		$con->join = "LEFT JOIN ".Acan_table_fate." b ON b.fid=a.bfid LEFT JOIN ".Acan_table_weibo_userinfo." i ON b.oid=i.oid";
		$data = $mbind->ofindAll($con);
		$this->recv($data);
	}
	function Act_users_photos(){
		if($this->uid<1){$this->recv(false);}
		$post = $_POST;
		if($post['oid']>0){
			$oid = $post['oid'];
			$data = Afun_new("weibo")->users_photos($oid);
		}else{
			$data = '';
		}
		$this->recv($data);
	}
}

?>